#!/usr/bin/php -q
<?PHP
/* Copyright 2005-2024, Lime Technology
 * Copyright 2012-2024, Bergware International.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version 2,
 * as published by the Free Software Foundation.
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 */
?>
<?
$docroot ??= ($_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp');
require_once "$docroot/webGui/include/Helpers.php";
extract(parse_plugin_cfg('dynamix',true));

// add translations
$_SERVER['REQUEST_URI'] = 'dashboard';
$login_locale = _var($display,'locale');
require_once "$docroot/webGui/include/Translations.php";

$boot   = "/boot/config/plugins/dynamix";
$file   = $argv[1];
$cmodel = is_file("$boot/$file") ? file_get_contents("$boot/$file") : '';
?>
<style>
div.case-list{float:left;padding:10px;margin:0 45px 64px 0;height:128px;width:128px;text-align:center;cursor:pointer}
div.case-list span{position:relative;top:64px;width:auto;max-width:128px;height:128px;font-size:128px}
div.case-list span.fa{top:24px;max-width:80px;font-size:80px}
div.case-list:hover{color:#f0000c}
div.case-name{position:relative;top:74px;font-family:clear-sans!important}
div.custom-name{position:relative;top:10px;font-family:clear-sans!important}
input#file{display:none}
</style>

<script>
function selectDone() {
  $('.sweet-alert').hide('fast').removeClass('nchan');
  swal.close();
  getCase();
}
function setCase(model) {
  $.post('/webGui/include/SelectCase.php',{mode:'set',file:'<?=$file?>',model:model},function(){selectDone();});
}
function importFile(file) {
  if (file.name.split('.').pop().toLowerCase() != 'png') return;
  var reader = new FileReader();
  reader.readAsDataURL(file);
  reader.onload = function(e){$.post('/webGui/include/SelectCase.php',{mode:'file',file:'<?=$file?>',data:e.target.result},function(){selectDone();})};
}
</script>

<div>
<?
$cases = file("$docroot/webGui/styles/default-cases.css",FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
foreach ($cases as $case) if (substr($case,0,6)=='.case-') $models[] = substr($case,1,strpos($case,':')-1);
natsort($models);
for ($i=0; $i < count($models); $i++) {
  $model = $models[$i];
  $name = substr($model,5);
  $title = str_replace('3u-avs-10-4','3u-avs-10/4',$name);
  $select = $name==$cmodel ? 'color:#e68a00' : '';
  echo "<div id='$name' class='case-list' style='$select' onclick='setCase(\"$name\")'><span class='$model'></span><div class='case-name'>$title</div></div>\n";
}
$select = $cmodel=='case-model.png' ? 'color:#e68a00' : '';
?>
<div id='Custom' class='case-list' style='<?=$select?>' onclick='$("input#file").trigger("click")'><span class='fa fa-file-image-o'></span><div class='custom-name'><?=_('custom image')?></div></div>
</div></div>
<input type='file' id='file' accept='.png' onchange='importFile(this.files[0])'>
</div>
